package com.apexedu.portal.util.umail;

import com.alibaba.fastjson.JSON;
import com.apexedu.portal.util.umail.bjxml.xstream.BJXMLUtil;
import com.apexedu.portal.util.umail.constant.UMailConstant;
import com.apexedu.portal.util.umail.service.UMailService;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by luchen on 2015-12-23.
 */
public class UMailUtil {
    public static String login(String username, String password) {
        Map paramMap = new HashMap();
        paramMap.put("username", username);
        paramMap.put("password", password);
        paramMap.put("domain", UMailConstant.MAILBOX_DOMAIN);
        String result = UMailService.login(paramMap);
        result = JSON.toJSONString(BJXMLUtil.xml2Map(result, "result"));
        return result;
    }

    public static String getMailbox(String keyword) {
        Map paramMap = null;
        paramMap = new HashMap();
        paramMap.put("username", UMailConstant.MAILAPP_ADMIN_USERNAME);
        paramMap.put("password", DigestUtils.md5Hex(UMailConstant.MAILAPP_ADMIN_PASSWORD));
        paramMap.put("domain", UMailConstant.MAILBOX_DOMAIN);
        paramMap.put("keyword", keyword);
        String result = UMailService.getMailbox(paramMap);
        result = JSON.toJSONString(BJXMLUtil.xml2Map(result, "result"));
        return result;
    }

    public static String addMailbox(String account, String pwd, String realname) {
        Map paramMap = null;
        paramMap = new HashMap();
        paramMap.put("username", UMailConstant.MAILAPP_ADMIN_USERNAME);
        paramMap.put("password", DigestUtils.md5Hex(UMailConstant.MAILAPP_ADMIN_PASSWORD));
        paramMap.put("domain", UMailConstant.MAILBOX_DOMAIN);
        paramMap.put("account", account);
        paramMap.put("pwd", pwd);
        paramMap.put("realname", realname);
        String result = UMailService.addMailbox(paramMap);
        result = JSON.toJSONString(BJXMLUtil.xml2Map(result, "result"));
        return result;
    }

    public static String modMailbox(String account, String pwd, String realname, String disabled) {
        Map paramMap = null;
        paramMap = new HashMap();
        paramMap.put("username", UMailConstant.MAILAPP_ADMIN_USERNAME);
        paramMap.put("password", DigestUtils.md5Hex(UMailConstant.MAILAPP_ADMIN_PASSWORD));
        paramMap.put("domain", UMailConstant.MAILBOX_DOMAIN);
        paramMap.put("account", account);
        paramMap.put("pwd", pwd);
        paramMap.put("realname", realname);
        paramMap.put("disabled", disabled);
        String result = UMailService.modMailbox(paramMap);
        result = JSON.toJSONString(BJXMLUtil.xml2Map(result, "result"));
        return result;
    }

    public static String getMail(String email, String password, String domain) {
        Map paramMap = new HashMap();
        paramMap.put("email", email);
        paramMap.put("password", password);
        paramMap.put("domain", domain);
        String result = UMailService.getMail(paramMap);
        result = JSON.toJSONString(BJXMLUtil.xml2Map(result, "result"));
        return result;
    }

    public static String getUnreadNumber(String email, String password, String domain){
        String msg=getMail(email,password,domain);
        msg=msg.substring(msg.indexOf("\"unread\":"));
        msg=msg.substring(0, msg.indexOf("}"));
        msg=msg.replace("\"", "");
        String[] s=msg.split(":");
        return  s[1];
    }
    public static void main(String[] args) {
        String msg=getMail("shghxyzsbgs@shghxy.com", "shghxyzsbgsshgh_2015", "shghxy.com");
        System.out.println(msg);
        msg=msg.substring(msg.indexOf("\"unread\":"));
        msg=msg.substring(0,msg.indexOf("}"));
        msg=msg.replace("\"", "");
        String[] s=msg.split(":");
        System.out.println(s[1]);
    }
}
